package mspbots.autotask.client.core;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;
import mspbots.autotask.client.core.client.AutoTaskClient;
import mspbots.autotask.client.core.client.ClientProperties;
import mspbots.autotask.common.RabbitKeys;
import mspbots.autotask.common.RedisKeys;
import mspbots.autotask.common.ResultMsg;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * mspbots.core.autotask.core.servicecall.SyncService
 *
 * @author <a href="https://github.com/vnobo">Alex bob</a>
 * @date Created by 2020/7/10
 */
@Log4j2
public abstract class AbstractToolsUtil {

    protected AutoTaskClient autoTaskClient;
    protected ObjectMapper objectMapper;
    protected ReactiveStringRedisTemplate stringRedisTemplate;
    protected ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;

    private RabbitMessagingTemplate rabbitMessagingTemplate;

    protected Mono<ClientProperties> loadProperties(Long tenantId) {
        return this.reactiveRedisTemplate.opsForValue()
                .get(RedisKeys.TENANT_PROPERTIES_REDIS_KEY_PREFIX + tenantId)
                .map(data -> this.objectMapper.convertValue(data, ClientProperties.class));
    }

    protected Mono<String> readAnchorId(String key) {
        return this.stringRedisTemplate.opsForValue().get(key)
                .defaultIfEmpty("0");
    }

    protected void recordAnchorId(String key, Long id) {
        this.readAnchorId(key)
                .filter(old -> Long.parseLong(old) < id)
                .flatMap(v -> this.stringRedisTemplate.opsForValue()
                        .set(key, String.valueOf(id), Duration.ofDays(7)))
                .subscribe();
    }

    protected Mono<String> readAnchorTime(String key) {
        return this.stringRedisTemplate.opsForValue().get(key)
                .defaultIfEmpty(LocalDateTime.now().minusYears(1).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    }

    protected void recordAnchorTime(String key, String value) {
        LocalDateTime nowAnchorTime = LocalDateTime.parse(value, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        this.readAnchorTime(key)
                .filter(v -> LocalDateTime.parse(v, DateTimeFormatter.ISO_LOCAL_DATE_TIME).isBefore(nowAnchorTime))
                .flatMap(v -> this.stringRedisTemplate.opsForValue()
                        .set(key, nowAnchorTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME), Duration.ofDays(7)))
                .subscribe();
    }

    protected void sendEntity(String key, ResultMsg resultMsg) {
        this.rabbitMessagingTemplate.convertAndSend(RabbitKeys.BASE_EXCHANGE_NAME, key, resultMsg);
    }

    @Autowired
    public void setAutoTaskClient(AutoTaskClient autoTaskClient) {
        this.autoTaskClient = autoTaskClient;
    }

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @Autowired
    public void setStringRedisTemplate(ReactiveStringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Autowired
    public void setReactiveRedisTemplate(ReactiveRedisTemplate<String, Object> reactiveRedisTemplate) {
        this.reactiveRedisTemplate = reactiveRedisTemplate;
    }

    @Autowired
    public void setRabbitMessagingTemplate(RabbitMessagingTemplate rabbitMessagingTemplate) {
        this.rabbitMessagingTemplate = rabbitMessagingTemplate;
    }

}
